package com.gitee.wsl.net.server.role.login

import com.gitee.wsl.net.exceptions.Exceptions
import com.gitee.wsl.net.server.role.admin.AdminsService
import com.gitee.wsl.net.server.role.admin.SessionService
import com.gitee.wsl.net.server.role.admin.SessionUser
import com.gitee.wsl.net.server.role.admin.checkRoleGuest
import io.ktor.server.application.call
import io.ktor.server.request.receive
import io.ktor.server.response.respond
import io.ktor.server.routing.Route
import io.ktor.server.routing.post
import io.ktor.server.sessions.sessions
import io.ktor.server.sessions.set


fun Route.login(sessionService: SessionService,
                adminsService: AdminsService
) {
    // test method websocket
    /*get("/test-websocket-4jdLXlY3V1QYt6Jsr7Yh") {
        // send message
        call.sendMessageSocket(
            NotificationResponse(
                action = NotificationAction.HELP_CHANGE,
            )
        )
        // response
        call.respond(HttpStatusCode.OK)
    }*/


    post("/login") {

        // check role
        call.checkRoleGuest()

        // get request
        val request = call.receive<Request>()

        // act
        /*val response = adminsService.transaction {
            adminsService.findUserByAuth(
                email = request.email,
                password = request.password
            ) ?: throw Exceptions.Unauthorized()
        }

        call.sessions.set(
            SessionUser(
                userId = response.id,
                role = response.role.name,
                token = sessionService.generateToken(response.id),
            )
        )

        // response
        call.respond(response)*/
    }
}